//
//  PPChoseFriendTableViewController.m
//  pet
//
//  Created by 陈无忌 on 9/23/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

//改页面是在创建群组时候使用
#import "PPInviteFriendTableViewController.h"
#import "PPInviteFriendCell.h"
#import "PPInvitePreviewCell.h"

#import "PPSystemConfiguraton.h"

@interface PPInviteFriendTableViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (weak, nonatomic) PPSystemConfiguraton *config;
@property (strong, nonatomic) NSMutableArray *friendList;
@property (strong, nonatomic) NSMutableArray *inviteList;
@property (weak, nonatomic) IBOutlet UICollectionView *inviteCollectionView;

@property (strong, nonatomic) NSMutableArray *inviteUserList;

@end

@implementation PPInviteFriendTableViewController

- (NSMutableArray *)inviteUserList
{
    if (!_inviteUserList) {
        _inviteUserList = [NSMutableArray array];
    }
    return _inviteUserList;
}

-(PPSystemConfiguraton *)config
{
    if (!_config) {
        _config = [PPSystemConfiguraton sharedSystemConfiguration];
    }
    return _config;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.inviteCollectionView.delegate = self;
    self.inviteCollectionView.dataSource = self;
    
    [self getFriendList];
    
}

-(void)getFriendList
{
    self.friendList = [self.config.friendArray mutableCopy];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.friendList.count;
}


-(BOOL)isEditing{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleInsert;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PPInviteFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"inviteCell"];
    PPUserInfo *user = [self.friendList objectAtIndex:indexPath.row];
    cell.userName.text = user.userName;
    [cell.avatar setImageWithURL:[PPImageUrl getImageUrlOriginal:user.userAvatar] placeholderImage:[UIImage imageNamed:@"defaultAvatar"] usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"select row%d",indexPath.row);
    PPUserInfo *user = [self.friendList objectAtIndex:indexPath.row];
    [self.inviteUserList addObject:user];
    [self.inviteCollectionView reloadData];
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSLog(@"unselect row%d",indexPath.row);
    PPUserInfo *user = [self.friendList objectAtIndex:indexPath.row];
    [self.inviteUserList removeObject:user];
    [self.inviteCollectionView reloadData];
}



#pragma mark - Collection view data source
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.inviteUserList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PPInvitePreviewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"previewCell" forIndexPath:indexPath];
    PPUserInfo *user = [self.inviteUserList objectAtIndex:indexPath.row];
    [cell.avatar setImageWithURL:[PPImageUrl getImageUrlOriginal:user.userAvatar] placeholderImage:[UIImage imageNamed:@"defaultAvatar"] usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    return cell;
}



- (IBAction)inviteButton:(UIBarButtonItem *)sender {
    self.inviteList = [[NSMutableArray alloc] init];
    
    NSArray *choseArray = [self.tableView indexPathsForSelectedRows];
    if (!choseArray.count > 0) {
        [PPHUD hudShowWithString:@"你没有邀请任何人" andView:self.view];
        return;
    }
    for (NSIndexPath *indexPath in choseArray) {
        PPUserInfo *user = [self.friendList objectAtIndex:indexPath.row];
        [self.inviteList addObject:[NSString stringWithFormat:@"%d",user.id]];
    }
    NSString *userIdString = @"";
    for (int i = 0;i < self.inviteList.count;i++ ) {
        NSString *idString = [self.inviteList objectAtIndex:i];
        userIdString = [userIdString stringByAppendingString:idString];
        userIdString = [userIdString stringByAppendingString:@","];
    }
    userIdString = [userIdString substringWithRange:NSMakeRange(0, userIdString.length - 1)];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"getInviteMember" object:nil userInfo:@{@"memberIdList":userIdString,@"memberIdArray":self.inviteList}];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
